﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Raven.Client.Linq;
using TimeTracker.Models;
using TimeTracker.Web.Models;
using TimeTracker.Persistence;
using TimeTracker.Entities;
using TimeTracker.Web.Mvc;

namespace TimeTracker.Web.Controllers
{
    public class EntryController : Controller
    {
        public ActionResult Create()
        {
            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;

                var model = EntryModel.Create(db.GetUserByEmail("my-user@mailinator.com"), 
                                              db.GetClientByName("Corvalius"), 
                                              db.GetProjectByName("Codecamp"));
                return PartialView(model);
            }
        }

        [HttpPost]
        public ActionResult Create(EntryModel model)
        {
            if (!ModelState.IsValid)
                return PartialView(model);

            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;
                var entry = model.ToModel();
                db.Store(entry);
                db.SaveChanges();
            }

            return Content("Ok");
        }

        public ActionResult Edit(string id)
        {
            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;
                var entry = db.GetEntryById(id);
                var model = EntryModel.FromModel(entry, db.GetClientById(entry.ClientId), db.GetProjectById(entry.ProjectId));

                return PartialView(model);
            }
        }

        [HttpPost]
        public ActionResult Edit(EntryModel model)
        {
            if (!ModelState.IsValid)
                return PartialView(model);

            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;

                var entry = db.GetEntryById(model.Id);
                model.UpdateModel(entry);
                
                db.SaveChanges();
            }

            return Content("Ok");
        }

        public ActionResult View(string id)
        {
            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;

                var entry = db.GetEntryById(id);

                var model = EntryModel.FromModel(entry, db.GetClientById(entry.ClientId), db.GetProjectById(entry.ProjectId));

                return PartialView(model);
            }
        }

        public ActionResult GetAll()
        {
            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;

                User user = db.GetUserByEmail("my-user@mailinator.com");

                var query = from e in db.Query<Entry>()
                            where e.UserId == user.Id
                            select e;

                var entries = new List<EntryModel>();
                foreach (var entry in query)
                {
                    var model = EntryModel.FromModel(entry, db.GetClientById(entry.ClientId), db.GetProjectById(entry.ProjectId));

                    entries.Add(model);
                }

                return new JsonNetResult
                           {
                               Data = entries, 
                               // JsonRequestBehavior = JsonRequestBehavior.AllowGet
                           };
            }
        }

        public ActionResult Continue(string id)
        {
            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;

                var entry = db.GetEntryById(id);
                if (!entry.IsActive)
                {
                    if (entry.LastStartTime.Date != DateTime.Now.Date)
                    {
                        entry = new Entry(entry);
                        db.Store(entry);                                            
                    }

                    entry.LastStartTime = DateTime.Now;    
                    entry.IsActive = true;
                    db.SaveChanges();
                }

                return new JsonNetResult
                {
                    Data = EntryModel.FromModel(entry, db.GetClientById(entry.ClientId), db.GetProjectById(entry.ProjectId)),
                    // JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }   
        }

        public ActionResult Stop(string id)
        {
            using (var context = new TimeTrackerContext())
            {
                var db = context.Database;

                var entry = db.GetEntryById(id);
                if (entry.IsActive)
                {
                    entry.ElapsedTime = entry.ElapsedTime.Add(DateTime.Now.Subtract(entry.LastStartTime));
                    entry.IsActive = false;                    

                    db.SaveChanges();
                }

                return new JsonNetResult
                {
                    Data = EntryModel.FromModel(entry, db.GetClientById(entry.ClientId), db.GetProjectById(entry.ProjectId)),
                    // JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }   
        }

    }

}

